package Text::Pipe::Translate;

use strict;
use warnings;
use Lingua::Translate;


our $VERSION = '0.03';


use base qw(Text::Pipe::Base);


__PACKAGE__->mk_scalar_accessors(qw(from to translators));


use constant DEFAULTS => (
    translators => {},
);


sub get_translator {
    my $self = shift;
    $self->translators->{ $self->from }{ $self->to } ||=
        Lingua::Translate->new(src => $self->from, dest => $self->to);
}


sub filter_single {
    my ($self, $input) = @_;
    $self->get_translator->translate($input);
}


1;


__END__

{% USE p = PodGenerated %}

=head1 NAME

{% p.package %} - Translate text from one language to another

=head1 SYNOPSIS

    use Text::Pipe 'pipe';
    my $pipe = pipe 'Translate::Babelfish', from => 'en', to => 'de';
    my $german = $pipe->filter('My hovercraft is full of eels.'),

=head1 DESCRIPTION

This pipe segment can translate text from one language to another. To do so it
uses L<Lingua::Translate>.

Both source and destination language are to be given in RFC3066 form. See
L<I18N::LangTags>.

=head1 METHODS

=over 4

{% p.write_methods %}

=item get_translator

Constructs a translator object for the current settings of C<from()> and
C<to()> and caches it using the C<translators()> accessor.

=item filter_single

Takes a string and translates it according to the current settings of
C<from()> and C<to()>.

=back

{% p.write_inheritance %}

{% PROCESS standard_pod %}

=cut

